package ch.subgate.pongwars;

public class GameHelper {
	public static float getDICFromSC(GameContext context, float scx) {
		return (scx - context.battlefieldLeft) / context.unitWidth;
	}
	
	public static float mirrorDirectionXAxis(float direction) {
		if (direction >= 0.0f) {
			direction = (float)Math.PI - direction;
		}
		else {
			direction = -(float)Math.PI - direction;
		}
		return direction;
	} // mirrorDirectionXAxis
	
	public static void applyBounce(Ball ball, Player playerRed, Player playerBlue) {
		// Battlefield bounce
		if (ball.mX > Constants.DIC_BATTLEFIELD_WIDTH - 0.5f) {
			// Ball is colliding with battlefield right
			ball.mX = Constants.DIC_BATTLEFIELD_WIDTH - 0.5f;
			ball.direction = GameHelper.mirrorDirectionXAxis(ball.direction);
		}
		else if (ball.mX < 0.5f) {
			// Ball is colliding with battlefield left
			ball.mX = 0.5f;
			ball.direction = GameHelper.mirrorDirectionXAxis(ball.direction);
		}
		
		// Player bounce
		if (ball.mY < playerRed.mY + 1.0f && ball.mY > playerRed.mY - 1.0f) {
			if (
					ball.mX > playerRed.mX - playerRed.mSize - 0.5f
					&& ball.mX < playerRed.mX + playerRed.mSize + 0.5f) {
				ball.bounce(playerRed);
			}
		}
		else  if (ball.mY > playerBlue.mY - 1.0f && ball.mY < playerBlue.mY + 1.0f) {
			if (
					ball.mX > playerBlue.mX - playerBlue.mSize - 0.5f
					&& ball.mX < playerBlue.mX + playerBlue.mSize + 0.5f) {
				ball.bounce(playerBlue);
			} // if
		} // if
	} // applyBounce
} // GameHelper
